package com.glsc.ngateway.opmanage.service.settlement;

import com.glsc.ngateway.common.api.common.utils.DateUtil;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.settlement.SettlementLog;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.settlement.SettlementLogRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.*;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class SettlementLogService {


    @Resource
    private SettlementLogRepository settlementLogRepository;


    /**
     * 根据清算人姓名和日期查询完成的条数
     *
     * @param createName
     * @param startTime
     * @param endTime
     * @return
     */
    public Integer findDataCount(String createName, LocalDateTime startTime, LocalDateTime endTime) {
        return settlementLogRepository.findDataCount(createName, startTime, endTime);
    }

    /**
     * 分页
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<SettlementLog> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<SettlementLog> page = settlementLogRepository.findAll(SpecificationUtil.buildSpecification(param, SettlementLog.class), pageable);
        return page;
    }

    /**
     * 根据清算人姓名和日期查询所有清算数据
     *
     * @param createName
     * @param startTime
     * @param endTime
     * @return
     */
    public List<SettlementLog> listByCreateNameAndDate(String createName, LocalDateTime startTime, LocalDateTime endTime) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";
        Map<String, Object> param = new HashMap<>();

        List<SettlementLog> list = settlementLogRepository.findAll(SpecificationUtil.buildSpecification(param, SettlementLog.class));
        return list;
    }

    /**
     * 获取最新五条数据
     *
     * @return
     */
    public List<SettlementLog> listNew5DataList() {
        Page<SettlementLog> page = page(1, 5, new HashMap<>());

        if (Objects.isNull(page) || CollectionUtils.isEmpty(page.getContent())) {
            return null;
        }
        return page.getContent();
    }

    /**
     * 保存
     *
     * @return
     */
    public SettlementLog save(User user) {
        List<SettlementLog> logs = settlementLogRepository.findAll(Example.of(SettlementLog.builder().settlementDate(DateUtil.getNowDateString()).build()));
        Assert.isTrue(CollectionUtils.isEmpty(logs), "今日清算已完成");
        SettlementLog log = SettlementLog.builder().settlementDate(DateUtil.getNowDateString()).createId(user.getId()).createName(user.getName()).build();
        return settlementLogRepository.save(log);
    }

    /**
     * 删除
     *
     * @param settlementLog
     */
    public void deleteById(SettlementLog settlementLog) {
        if (Objects.isNull(settlementLog.getId())) {
            return;
        }
        settlementLogRepository.deleteById(settlementLog.getId());
    }


}
